import { createRouter, createWebHistory } from 'vue-router';
import {userUserStore} from "@/stores/index.js";
import NProgress from 'nprogress'   // 导入 nprogress
import "@/utils/nprogress.css"

const router = createRouter({
    history: createWebHistory('/'),
    routes: [
        {
            path: '/register',
            component: () => import('@/views/login/LoginLayout.vue'),
            children: [
                {
                    path: '/register',
                    name: 'register-page',
                    component: () => import('@/views/login/RegisterPage.vue')
                },
                {
                    path: '/login',
                    name: 'login-page',
                    component: () => import('@/views/login/LoginPage.vue')
                }
            ]
        },
        {
            path: '/setdetailinfo',
            name: 'setDetailInfo',
            component: () => import('@/views/detailInfo/SetDetailInfoLayout.vue')
        },
        {
            path: '/setcompanyinfo',
            name: 'setCompanyInfo',
            component: () => import('@/views/detailInfo/SetCompanyInfoLayout.vue')
        },
        {
            path: '/jobinfo',
            name: 'jobInfoDisplay',
            component: () => import('@/views/jobInformation/JobInfoLayout.vue')
        },
        {
            path: '/message',
            name: 'messageBox',
            component: () => import('@/views/messageBox/MessageBoxPage.vue')
        },
        {
            path: '/companyinfo',
            name: 'companyinfo',
            component: () => import('@/views/companyInfo/CompanyInfoPage.vue')
        },

        {
            path: '/',
            name: 'home',
            component: () => import('@/views/home/HomeView.vue')
        },
        {
            path: '/personal',
            name: 'personal',
            component: () => import('../views/personal/Personal.vue'),
            children: [
                {
                    path: '',
                    component: () => import('../views/personal/components/Basic.vue')
                },
                {
                    path: 'schedule',
                    component: () => import('../views/personal/components/Schedule.vue')
                },
                {
                    path: 'resume',
                    component: () => import('@/views/resumeCreation/ResumeCreationPage.vue')
                },
                {
                    path: 'attachment',
                    component: () => import('@/views/resumeCreation/components/AttachmentResume.vue')
                },
                {
                    path: 'time',
                    component: () => import('@/views/interviewScheduler/InterviewScheduler.vue')
                },
                {
                    path: 'security',
                    component: () => import('@/views/accountSettings/AccountSettingsPage.vue')
                },
            ]
        },
        {
            path: '/postDetail',
            name: 'postDetail',
            component: () => import('@/views/postDetail/DetailView.vue')
        },
        {
            path: '/forum',
            name: 'forum',
            component: () => import('@/views/forum/Forum.vue')
        },
    ]
});

NProgress.configure({
    easing: 'ease',  // 动画方式，和css动画属性一样（默认：ease）
    speed: 500,  // 递增进度条的速度，单位ms（默认： 200）
    showSpinner: false, // 是否显示加载ico
    trickle: true,//是否自动递增
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3, // 初始化时的最小百分比，0-1（默认：0.08）
    container: 'body',
})



router.beforeEach((to, from, next) => {
    // 如果没有token，且访问非登录页
    NProgress.start();
    const userStore = userUserStore();
    if (!userStore.token && ![ '/login', '/register', '/setdetailinfo','/setcompanyinfo' ].includes(to.path)) {
        next('/login');
    } else {
        next();
    }
});

router.afterEach(() => {
    NProgress.done()
})

export default router;
